






import { ArrIndexOf } from '@/assets/js/Tools'
import { getMusicLyricMp3 } from '@/api/music'
import { Toast, Dialog } from 'vant'
import { getError } from '@/api/music'
import { v } from '../main'



export default {
    state : {
        myAudio : {
            isshow : false,//当前音乐播放状态
            audio : document.createElement('audio'),//
            isPl : false,//
            data : JSON.parse(localStorage.getItem('MyaudioData')) || [],//
            index : parseInt(localStorage.getItem('Myaudioindex')) || 0,//
            bofIndex : 0,//
            duration : 0,//音乐总时长
            currentTime : 0,//音乐当前播放时间
            $currentRate : 0,//当前播放进度小数
            currentRate : 0,//当前播放进度百分比
            gec : [],//歌词 
            imgUrl : localStorage.getItem('MyaudioimgUrl') || ''//
        },
        MyList : {
            isshow : false,//是否显示音乐列表
        },
        MyDateil : {
            gecIndex : 0,//歌曲播放位置对应的歌词位置
        },
    },
    mutations : {
        // 保存data
        saveMyAudioData({ myAudio }){
            localStorage.setItem('MyaudioData', JSON.stringify(myAudio.data));
        },
        // 保存Index
        saveMyAudioIndex({ myAudio }){
            localStorage.setItem('Myaudioindex', myAudio.index);
        },
        // 保存ImgUrl
        saveMyAudioImgUrl({ myAudio }){
            localStorage.setItem('MyaudioimgUrl', myAudio.imgUrl);
        },
        //当播放的音乐总时长发生更改时
        onDurationchange({ myAudio, MyDateil }) {
            myAudio.duration = myAudio.audio.duration
            //刷新歌曲详情(是否喜欢)；
            v.$emit('pushDetail');
        },
        // 播放时间改变时
        timeupdate({ myAudio }) {
            myAudio.currentTime = myAudio.audio.currentTime;
            myAudio.$currentRate = myAudio.currentTime / myAudio.audio.duration
            myAudio.currentRate = myAudio.$currentRate * 100
            v.$emit('onBfAudio')
        },
        // 删除音乐列表的一个音乐
        DeleteAudioPlay({ myAudio }, x){
            if(x){
                let a = ArrIndexOf(myAudio.data,x);
                let d = myAudio.data[myAudio.index];
                myAudio.data.splice(a,1);
                if(myAudio.data.length !== 0){
                    if(a === myAudio.index){
                        this.commit('sunPlay', 0);
                    }else{
                        myAudio.index = ArrIndexOf(myAudio.data, d);
                        this.commit('saveMyAudioIndex');
                    }
                }else{
                    this.commit('onPause')
                }
                this.commit('saveMyAudioData');
            }
        },
        DeleteAudioALL({ myAudio }){
            Dialog.confirm({
                message: '确认删除所有歌曲吗？',
            }).then(() => {
                this.commit('onPause')
                myAudio.data = []
                if(v.$route.path == '/songDetail'){
                    v.$router.back(-1)
                }
                this.commit('saveMyAudioData');
            }).catch(()=>{})
        },
        //顺序播放函数
        sunPlay({ myAudio }, n = 1){
            if(myAudio.data.length > 0){
                myAudio.index += n;
                myAudio.index = myAudio.index < 0 ? myAudio.data.length - 1 : myAudio.index;
                myAudio.index %= myAudio.data.length;
                this.commit('getMusicData');
                this.commit('saveMyAudioIndex');
            }
        },
        //获取歌曲和歌词
        getMusicData({ myAudio, MyDateil }, x = true){
            getMusicLyricMp3(myAudio.data[myAudio.index].rid).then(res => {
                if(res.code == 200){
                    MyDateil.gecIndex = 0
                    myAudio.audio.src = res.data.mp3
                    myAudio.gec = res.data.lrc ? res.data.lrc : []
                    myAudio.imgUrl = myAudio.data[myAudio.index].pic
                    this.commit('saveMyAudioImgUrl');
                    if(x){
                        this.commit('play');
                    }
                }
            })
        },
        // 播放
        play({ myAudio }){
            if(myAudio.data[myAudio.index]){
                var e = myAudio.audio.play();
                e.catch((err) => {
                })
            }
        },
        //播放、暂停控制器
        onPlay({ myAudio }){
            if(!myAudio.isshow){
                if(myAudio.isPl){
                    this.commit('play');
                }else{
                    myAudio.isPl = true
                    this.commit('getMusicData');
                }
                
            }else{
                this.commit('onPause');
            }
        },
        //暂停
        onPause({ myAudio }){
            myAudio.audio.pause()
        },
        //切换播放方式
        onBof({ myAudio }){
            myAudio.bofIndex++;
            myAudio.bofIndex %= 3;
            Toast.success(myAudio.bofIndex == 0 ? '顺序播放' : myAudio.bofIndex == 1 ? '随机播放' : '单曲循环')
            if(myAudio.bofIndex == 2){
                myAudio.audio.loop = true;
            }else{
                myAudio.audio.loop = false;
            }
        },
        //添加一个新的音乐
        addAudio({ myAudio }, x){
            if(x){
                let a = ArrIndexOf(myAudio.data, x);
                if(a === -1){
                    myAudio.index = 0;
                    myAudio.data.unshift(x);
                    this.commit('saveMyAudioData');
                }else{
                    myAudio.index = a;
                }
                this.commit('getMusicData');
                this.commit('saveMyAudioIndex');
            }else{
                Toast.loading('网络异常')
            }
        },

        //添加所有音乐
        addAllAudio({ myAudio }, list){
            list.forEach(x => {
                let a = ArrIndexOf(myAudio.data, x);
                if(a === -1){
                    myAudio.data.push(x);
                }
            })
            this.commit('saveMyAudioData');
        },

        //初始化音乐播放器
        musicInit({myAudio }){
            myAudio.audio.onended = () => {
                if (myAudio.bofIndex == 0) {
                    this.commit('sunPlay', 1);
                } else if (myAudio.bofIndex == 1) {
                    this.commit('sunPlay');
                }
            }
            myAudio.audio.ondurationchange = () => {
                this.commit('onDurationchange')
            }
            myAudio.audio.ontimeupdate = () => {
                this.commit('timeupdate')
            }
            myAudio.audio.onerror = () => {
                Toast.loading("网络不稳定")
                getError(myAudio.data[myAudio.index].rid).then(res => {
                    if(res.code == 200){
                        this.commit('getMusicData')
                    }else{
                        Toast.fail('找不到支持的资源')
                    }
                })
            }
            myAudio.audio.onpause = () => {
                myAudio.isshow = !myAudio.audio.paused
            }
            myAudio.audio.onplay = () => {
                myAudio.isshow = !myAudio.audio.paused
            }
        }
    },
    actions : {

    }
}